package com.seocoo.worker.friend.adapter;

import android.graphics.Color;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.seocoo.worker.R;
import com.seocoo.worker.order.entity.RspOrderIWorkmateInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Ssp
 * e-mail : 981297079@qq.com
 * date   : 2020/3/720:46
 * desc   : 搜索工友
 * version: 1.0
 */
public class FriendSearchAdapter extends BaseQuickAdapter<RspOrderIWorkmateInfo, BaseViewHolder> {

    private SparseArray<RspOrderIWorkmateInfo> tempData = new SparseArray<>();
    private List<RspOrderIWorkmateInfo> haveData = new ArrayList<>();

    public FriendSearchAdapter() {
        super(R.layout.friend_search_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, RspOrderIWorkmateInfo item) {
        TextView tvContent = helper.getView(R.id.tvContent);
        TextView ivDistance = helper.getView(R.id.ivDistance);

        if (haveData.size() > 0) {
            //表示有已关联的工友
            ArrayList<String> strings = new ArrayList<>();
            for (RspOrderIWorkmateInfo bean : haveData) {
                strings.add(bean.getUserId() + bean.getRealName());
            }
            if (!strings.contains(item.getUserId() + item.getRealName())) {
                //未关联的好友
                tvContent.setTextColor(Color.parseColor("#333333"));
                ivDistance.setTextColor(Color.parseColor("#333333"));
            } else {
                //表示当前是已关联的好友
                tvContent.setTextColor(Color.parseColor("#999999"));
                ivDistance.setTextColor(Color.parseColor("#999999"));
            }

        } else {
            //未关联的好友
            tvContent.setTextColor(Color.parseColor("#333333"));
            ivDistance.setTextColor(Color.parseColor("#333333"));
        }


        View view = helper.getView(R.id.ivCheck);
        view.setSelected(item.isSelect());
        helper.setText(R.id.tvContent, item.getPhoneNumber() + "(" + item.getRealName() + ")")
                .setText(R.id.ivDistance, item.getDistance() + "km");
    }

    /**
     * 响应点击事件 刷新当前选择状态
     */
    public void setTempData(int position) {
        RspOrderIWorkmateInfo data = mData.get(position);
        if (haveData.size() > 0) {
            //表示有已关联的工友
            ArrayList<String> strings = new ArrayList<>();
            for (RspOrderIWorkmateInfo bean : haveData) {
                strings.add(bean.getUserId() + bean.getRealName());
            }
            if (!strings.contains(data.getUserId() + data.getRealName())) {
                //未选中过
                initChecked(position, data);
            }

        } else {
            initChecked(position, data);
        }

    }

    private void initChecked(int position, RspOrderIWorkmateInfo data) {
        if (tempData.size() == 0) {
            //没有选择过数据
            data.setSelect(true);
            tempData.append(data.getUserId().hashCode(), data);
            notifyItemChanged(position, data);
        } else {
            RspOrderIWorkmateInfo rspData = tempData.get(data.getUserId().hashCode());
            if (rspData == null) {
                //该数据没有添加过
                tempData.append(data.getUserId().hashCode(), data);
                data.setSelect(true);
                notifyItemChanged(position, data);
            } else {
                //该数据已经添加过 取消选中
                data.setSelect(false);
                tempData.remove(data.getUserId().hashCode());
                notifyItemChanged(position, data);
            }
        }
    }

    public SparseArray<RspOrderIWorkmateInfo> getTempData() {
        return this.tempData;
    }

    public void setHaveData(List<RspOrderIWorkmateInfo> haveData1) {
        this.haveData = haveData1;
    }
}
